package com.example.ch_71;

// Command 实现类
public class AdderCommend extends AbsCommand {

    private Adder adder;
    private int value;

    public AdderCommend() {
        super();
        this.adder = new Adder();
    }

    @Override
    public int execute(int value) {
        this.state = CommandState.DO;
        this.value = value;
        return this.adder.add(value);
    }

    @Override
    public int undo() {

        if (this.state == CommandState.IDLE) {
        } else if (this.state == CommandState.UNDO) {
            return this.execute(value);
        } else {
            return this.doUndoAction();
        }

        return 0;
    }

    public int doUndoAction() {
        this.state = CommandState.UNDO;
        return this.adder.add(-value);
    }
    
}
